package views.games;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import views.util.GameColors;

public class MemorizzaTabellaView extends GameView {

	private JButton[][] cells;
	private int dimension;
	private JPanel main;
	private JPanel tab;
	private GridBagConstraints gbc;
	private ActionListener currentActionListener;

	public MemorizzaTabellaView(int dimension) {
		super(GameColors.VERDE, GameColors.VERDE_CHIARO, "TURNO 1 su 15",
				"PUNTEGGIO: 0", "");
		this.dimension = dimension;
		// setLayout( new BorderLayout());
		main = new JPanel();
		main.setBackground(GameColors.VERDE);
		gbc = new GridBagConstraints();
		main.setLayout(new GridBagLayout());
		add(main, BorderLayout.CENTER);
		tab = newTab();
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.gridwidth = 3;
		// gbc.fill = GridBagConstraints.HORIZONTAL;
		main.add(tab, gbc);
		// JPanel bottom = new JPanel();
		// bottom.setLayout(new GridBagLayout());
		// bottom.setOpaque(false);
		// GridBagConstraints gbcBottom = new GridBagConstraints();
		// add( bottom, BorderLayout.SOUTH);
		// gbcBottom.insets = new Insets(5,3,5,2);
		// gbcBottom.gridx = 0;
		// gbcBottom.gridy = 0;
		// bottom.add(rounds, gbcBottom);
		// gbcBottom.gridx = 1;
		// gbcBottom.gridy = 0;
		// bottom.add(score, gbcBottom);
		// gbcBottom.gridx = 2;
		// gbcBottom.gridy = 0;
		// bottom.add(tiles, gbcBottom);
	}

	/**
	 * @param dim
	 */
	public void update(int dim) {
		dimension = dim;
		tab.setVisible(false);
		main.remove(tab);
		tab = newTab();
		main.add(tab, gbc);
	}

	public JButton[][] getCells() {
		return cells;
	}

	public int getDimension() {
		return dimension;
	}

	public void setDimension(int d) {
		dimension = d;
	}

	public JLabel getRounds() {
		return gameClock;
	}

	public void setRounds(int r) {
		gameClock.setText("TURNO " + r + " su 15");
		;
	}

	public JLabel getScore() {
		return score;
	}

	public void setScore(int s) {
		score.setText("PUNTEGGIO: " + s);
		;
	}

	public JLabel getTiles() {
		return other;
	}

	public void setTiles(int t) {
		other.setText("CELLE COLORATE " + t);
	}

	private JPanel newTab() {
		JPanel tab = new JPanel();
		cells = new JButton[dimension][dimension];
		Dimension dim = new Dimension(dimension * 70, dimension * 70);
		tab.setPreferredSize(dim);
		tab.setLayout(new GridLayout(dimension, dimension, 3, 3));
		tab.setBackground(GameColors.VERDE);
		for (int i = 0; i < dimension; i++)
			for (int j = 0; j < dimension; j++) {
				cells[i][j] = new JButton();
				cells[i][j].setName(i + "-" + j);
				// if ( j == i/2)
				// cells[i][j].setBackground(Color.orange);
				// else
				cells[i][j].setBackground(Color.white);
				cells[i][j].setName(i + "," + j);
				tab.add(cells[i][j]);
			}
		return tab;
	}// littleTab

	public void addActionListenerToTheCells(ActionListener a) {
		currentActionListener = a;
		for (int i = 0; i < dimension; i++)
			for (int j = 0; j < dimension; j++)
				cells[i][j].addActionListener(a);
	}

	@Override
	public void gameEnded() {
		other.setVisible(false);
		// score.setVisible(false);
		gameClock.setVisible(false);
		tab.setVisible(false);
		main.remove(tab);

		JLabel end = newLabel("Partita Conclusa!", GameColors.VERDE_CHIARO);
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridwidth = 4;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		main.add(end, gbc);
		gbc.gridy = 1;
		main.add(score, gbc);
		JButton restart = newButton("Ricomincia");
		JButton menu = newButton("Menu Giochi");
		gbc.gridy = 2;
		gbc.gridwidth = 2;
		gbc.gridx = 0;
		main.add(restart, gbc);
		gbc.gridx = 2;
		main.add(menu, gbc);
		restart.addActionListener(currentActionListener);
		menu.addActionListener(currentActionListener);
	}
}
